/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-10-16 08:22:07
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-16 08:24:57
 * @FilePath: /react18-vite-admin/src/service/biz/role/useRoleList.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useState } from 'react';

import { useStateRef } from '@/service/useStateRef';
import type { RoleListItem } from '@/typings/api/role';
import { RoleListParam, readRoleList } from '@/api/role';

export { RoleListParam }

export const useRoleList = () => {
    const [total, setTotal] = useState(0);
    const [list, setList] = useState<RoleListItem[]>([]);
    const [loading, loadingRef, setLoading] = useStateRef(false);

    const getList = (param: RoleListParam) => {
        if (loadingRef.current) return;
        setLoading(true);
        return readRoleList(param)
            .then((res) => {
                setList(res.data.records);
                setTotal(res.data.total);
                return res;
            })
            .finally(() => {
                setLoading(false);
            });
    };

    return {
        total,
        list,
        loading,
        getList
    };
};
